home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_074 / random / rnds.c next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  82 lines

  1. /************************************************************************/
  2. /*                rnds.c                                  */
  3. /*                                                                      */
  4. /*    This module contains functions used to generate random numbers.   */
  5. /* First call srand() with the seed values and then you can use rand()  */
  6. /* to get a random number.                                              */
  7. /*                                                                      */
  8. /* Thurdsay- March    19, 1987  5:19pm  new changes from April 87 BYTE  */
  9. /* Tuesday - February 17, 1987  8:30pm  change randx from UWORD to WORD */
  10. /* Monday  - December 29, 1986  7:00pm                                  */
  11. /*                                                          Gene Toole  */
  12. /************************************************************************/
  13.  
  14. /* ====================== Standard Amiga Includes ===================== */
  15. #include <exec/types.h>
  16. #include <math.h>
  17.  
  18. /* ====================== Functions for THIS Program ================== */
  19. VOID  srand();
  20. LONG rand();
  21. FLOAT random();
  22.  
  23. /* =========================== WORD Variables ========================= */
  24. STATIC WORD
  25.    x = 1,              /* default: global seeds */
  26.    y = 10000, 
  27.    z = 3000;
  28.  
  29. /* --------------------------------------------------------------------
  30.  * Here is the random number seed function.  You must send it 3 WORD
  31.  * values between 1 and 30000.  It will then set the global seed 
  32.  * values used by rand().
  33.  * If you do not call this function, the 3 default global seed values 
  34.  * will be used.
  35.  */
  36. VOID srand(xv, yv, zv)
  37. WORD
  38.    xv,
  39.    yv,
  40.    zv;
  41. {
  42.    x = xv;
  43.    y = yv;
  44.    z = zv;
  45. }
  46.  
  47. /* --------------------------------------------------------------------
  48.  * This is the actual random function.  It will return a random number 
  49.  * within the range of LONG min to max inclusive.  You must pass to it
  50.  * the min and max UWORD values for the range you wish returned.
  51.  */
  52. LONG rand(min, max)
  53. UWORD 
  54.    min, 
  55.    max;
  56. {
  57.    return ( (LONG)( random() * (FLOAT)(max - min + 1) + min ) );
  58. }
  59.  
  60. /* -------------------------------------------------------------------- 
  61.  * This is the function from the April 87 issue of BYTE, page 128.
  62.  * It returns a FLOAT in the range: 0 - (almost 1).
  63.  */
  64. FLOAT random()
  65. {
  66.    FLOAT
  67.       temp;
  68.  
  69.    if ( (x = 171 * (x % 177) - 2 * (x / 177)) < 0 )
  70.       x += 30269;
  71.       
  72.    if ( (y = 172 * (y % 176) - 35 * (y / 176)) < 0 )
  73.       y += 30307;
  74.    
  75.    if ( (z = 170 * (z % 178) - 63 * (z / 178)) < 0 )
  76.       z += 30323;
  77.       
  78.    temp = x/30269.0 + y/30307.0 + z/30323.0;
  79.    
  80.    return (temp - trunc(temp));
  81. }
  82.